// pages/topic_detail/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    user: '',
    topic: '',
    comments: '',
    commentInput: ''
  },
  // 点击提交评论按钮
  clickCommentBtn(e) {
    var that = this;
    var commentInput = that.data.commentInput;
    if (commentInput) {
      var user = wx.getStorageSync('user');
      var token = wx.getStorageSync('token')
      // 发表评论
      wx.request({
        url: "http://127.0.0.1:8080/health/comment",
        method: "post",
        header: {
          'content-type': 'application/json',  //这里注意POST请求content-type是小写，大写会报错  
          'Authorization': token
        },
        data: {
          topicId: this.data.id,
          content: commentInput,
          userName: user.userName,
          userId: user.userId
        },
        success(res) {
          if (res.data.code == 200) {
            wx.showToast({
              title: '评论成功',
              icon: 'success',
              duration: 2000
            })
            that.onLoad()
          }
        }
      });
    } else {
      wx.showToast({
        title: '请先输入评论',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = that.data.id;
    var user = wx.getStorageSync('user');
    if(options){
      id = options.id;
    }
    that.setData({
      user: user
    })
    var token = wx.getStorageSync('token');
    wx.request({
      url: 'http://127.0.0.1:8080/health/topic/'+id, // 获取话题详情接口
      method: 'GET',
      header: {
        'content-type': 'application/json',  //这里注意POST请求content-type是小写，大写会报错  
        'Authorization': token
      },
      success(res) {
        if (res.data.code==200) {
          that.setData({
            topic : res.data.data
          })
          wx.request({
            url: 'http://127.0.0.1:8080/health/comment/list?topicId='+id, // 获取话题评论详情接口
            method: 'GET',
            header: {
              'content-type': 'application/json',  //这里注意POST请求content-type是小写，大写会报错  
              'Authorization': token
            },
            success(res) {
              if(res.data.code==200) {
                that.setData({
                  id : id,
                  comments : res.data.rows
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: "初始化话题失败",
            icon: 'none',
          })
        }
      }
    })

  },

  // 输入评论
  commentInput(e){
    this.setData({
      commentInput: e.detail.value
    });
  },
  /// 删除评论
  deleteComment(e){
    var that = this;
    var user = wx.getStorageSync('user');
    var token = wx.getStorageSync('token');
    var comment = e.currentTarget.dataset.data
    var comments= this.data.comments;
    if(comment.userId == user.userId){
      wx.showModal({
        title: '提示',
        content: '确定要删除吗？',
        success: function (sm) {
          if (sm.confirm) {
            wx.request({
              url: 'http://127.0.0.1:8080/health/comment/'+comment.id, // 获取话题详情接口
              method: 'delete',
              header: {
                'content-type': 'application/json',  //这里注意POST请求content-type是小写，大写会报错  
                'Authorization': token
              },
              success(res) {
                console.log(res.data)
                if(res.data.code==200) {
                  for (var i = 0; i < comments.length; i++) {
                    if (comments[i].id == comment.id) {
                      comments.splice(i, 1);
                      that.setData({
                        comments: comments
                      })
                    }
                  }
                  wx.showToast({
                    title: "删除成功",
                    icon: 'none',
                  })
                }
              }
            })
            } else if (sm.cancel) {
              console.log('用户点击取消')
            }
          }
        })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})